﻿using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;

namespace WPF_Preview.PreviewClass
{
    public class WorksheetRenderer
    {
        public Bitmap RenderWorksheetToBitmap(ExcelWorksheet worksheet, List<(int, int)> cells)
        {
            var dimensions = worksheet.Dimension;
            int rows = dimensions.End.Row;
            int cols = dimensions.End.Column;
            float[] columnWidths = new float[cols];
            float[] rowHeights = new float[rows];
            using (Graphics graphics = Graphics.FromImage(new Bitmap(1, 1)))
            {
                var font = new Font("Arial", 15);
                for (int row = 1; row <= rows; row++)
                {
                    for (int col = 1; col <= cols; col++)
                    {
                        var cell = worksheet.Cells[row, col];
                        string text = cell.Text;
                        var textSize = graphics.MeasureString(text, font);
                        columnWidths[col - 1] = Math.Max(columnWidths[col - 1], textSize.Width + 20);
                        rowHeights[row - 1] = Math.Max(rowHeights[row - 1], textSize.Height + 20);
                    }
                }
            }
            float width = 0;
            foreach (var w in columnWidths)
            {
                width += w;
            }
            float height = 0;
            foreach (var h in rowHeights)
            {
                height += h;
            }
            Bitmap bitmap = new Bitmap((int)width, (int)height);
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                graphics.Clear(Color.AliceBlue);
                StringFormat format = new StringFormat
                {
                    Alignment = StringAlignment.Center,
                    LineAlignment = StringAlignment.Center
                };
                float currentX = 0;
                float currentY = 0;
                for (int row = 1; row <= rows; row++)
                {
                    currentX = 0;
                    var rowColor = row % 2 == 1 ? Color.White : Color.AliceBlue;
                    for (int col = 1; col <= cols; col++)
                    {
                        var cell = worksheet.Cells[row, col];
                        string text = cell.Text;
                        graphics.FillRectangle(new SolidBrush(rowColor),
                            currentX, currentY, columnWidths[col - 1], rowHeights[row - 1]);
                        graphics.DrawString(
                        text, new Font("Arial", 15),
                            new SolidBrush(cells.Contains((row, col)) ? Color.Red : Color.Black),
                            new RectangleF(currentX, currentY, columnWidths[col - 1], rowHeights[row - 1]),
                            format);
                        using (Pen pen = new Pen(Color.Black, 2))
                        {
                            graphics.DrawLine(pen, currentX, currentY + rowHeights[row - 1], currentX + columnWidths[col - 1], currentY + rowHeights[row - 1]);
                            graphics.DrawLine(pen, currentX + columnWidths[col - 1], currentY, currentX + columnWidths[col - 1], currentY + rowHeights[row - 1]);
                        }
                        currentX += columnWidths[col - 1];
                    }
                    currentY += rowHeights[row - 1];
                }
                using (Pen pen = new Pen(Color.Black, 2))
                {
                    graphics.DrawLine(pen, 0, 0, 0, height);
                    graphics.DrawLine(pen, width, 0, width, height);
                    graphics.DrawLine(pen, 0, 0, width, 0);
                    graphics.DrawLine(pen, 0, height, width, height);
                }
            }
            return bitmap;
        }
        public BitmapSource BitmapToBitmapSource(Bitmap bitmap)
        {
            using (var memoryStream = new MemoryStream())
            {
                bitmap.Save(memoryStream, ImageFormat.Png);
                memoryStream.Position = 0;
                var bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.UriSource = null;
                bitmapImage.StreamSource = memoryStream;
                bitmapImage.EndInit();
                bitmapImage.Freeze();
                return bitmapImage;
            }
        }
    }
}
